package cn.golxen.rsms.controller;

import cn.golxen.rsms.bean.bo.UserMainWithCodeBo;
import cn.golxen.rsms.bean.entity.EmployeeMain;
import cn.golxen.rsms.bean.entity.StoreMain;
import cn.golxen.rsms.bean.entity.StoreOffers;
import cn.golxen.rsms.bean.entity.UserMain;
import cn.golxen.rsms.commons.Result;
import cn.golxen.rsms.commons.utils.WxGetOpenIdUtils;
import cn.golxen.rsms.exception.exceptionClass.AddGoodsStoreException;
import cn.golxen.rsms.exception.exceptionClass.AddStoreException;
import cn.golxen.rsms.service.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping(value = "/store")
public class StoreController {
    @Autowired
    StoreService storeService;

    @PostMapping(value = "/sendRegisterMail")
    public Result sendRegisterMail(@RequestBody UserMain userMain){
        storeService.sendRegisterMail(userMain.getId(), userMain.getMail());
        return Result.successResult();
    }

    @PostMapping(value = "/registerUser")
    public Result registerUser(@RequestBody UserMainWithCodeBo bo){
        storeService.registerUser(bo);
        return Result.successResult();
    }

    @PostMapping(value = "/loginUser")
    public Result loginUser(@RequestBody UserMainWithCodeBo bo){
        return Result.successResult(storeService.loginUser(bo));
    }

    @PostMapping(value = "/update/bindOpenId")
    public Result bindOpenId(@RequestBody UserMain userMain){
        return Result.successResult(storeService.bindOpenId(userMain.getOpenId(), userMain.getId()));
    }

    @PostMapping(value = "/loginUserOnSendMail")
    public Result loginUserOnSendMail(@RequestBody UserMain userMain){
        storeService.loginUserOnSendMail(userMain);
        return Result.successResult();
    }

    @GetMapping(value = "/get/userMainByOpenId/{openId}")
    public Result getUserMainByOpenId(@PathVariable String openId){
        return Result.successResult(storeService.getUserMainByOpenId(openId));
    }

    @GetMapping(value = "/get/OpenIdByWxCode/{code}")
    public Result getOpenIdByWxCode(@PathVariable String code){
        return Result.successResult(WxGetOpenIdUtils.getOpenId(code));
    }

    @PostMapping(value = "/loginEmployeeMain")
    public Result loginEmployeeMain(@RequestBody EmployeeMain employeeMain){
        return Result.successResult(storeService.loginEmployeeMain(employeeMain));
    }

    @PostMapping(value = "/sendCommonMail")
    public Result sendCommonMail(@RequestBody UserMain userMain){
        storeService.sendCommonMail(userMain.getId(), userMain.getMail());
        return Result.successResult();
    }

    @PostMapping(value = "/add/store")
    public Result addStore(@RequestBody StoreMain bo){
        int id=storeService.addStore(bo);
        if (id==0)
            throw new AddStoreException();
        return Result.successResult(id);
    }
    @GetMapping(value = "/del/{storeId}/{userId}/{mailCode}")
    public Result delStore(@PathVariable int storeId,@PathVariable String userId,@PathVariable String mailCode){
        return Result.successResult(storeService.delStore(storeId,userId,mailCode));
    }
    @PostMapping(value = "/update/store")
    public Result updateStore(@RequestBody StoreMain storeMain){
        return Result.successResult(storeService.updateStore(storeMain));
    }

    @GetMapping(value = "/get/storeList/{userId}")
    public Result getStoreMainList(@PathVariable String userId){
        return Result.successResult(storeService.getStoreMainList(userId));
    }

    @PostMapping(value = "/add/employee")
    public Result addEmployee(@RequestBody EmployeeMain employeeMain){
        return Result.successResult(storeService.addEmployee(employeeMain));
    }

    @PostMapping(value = "/del/employee")
    public Result delEmployee(@RequestBody EmployeeMain employeeMain){
        return Result.successResult(storeService.delEmployee(employeeMain));
    }

    @PostMapping(value = "/update/employee")
    public Result updateEmployee(@RequestBody EmployeeMain employeeMain){
        return Result.successResult(storeService.updateEmployee(employeeMain));
    }

    @GetMapping(value = "/get/employee/{storeId}")
    public Result getEmployeeMainList(@PathVariable int storeId){
        return Result.successResult(storeService.getEmployeeList(storeId));
    }

    @PostMapping(value = "/add/storeOffers")
    public Result addStoreOffers(@RequestBody StoreOffers storeOffers){
        return Result.successResult(storeService.addStoreOffers(storeOffers));
    }

    @GetMapping(value = "/get/StoreOffersListHistory/{storeId}")
    public Result getStoreOffersListHistory(@PathVariable int storeId){
        return Result.successResult(storeService.getStoreOffersListHistory(storeId));
    }

    @GetMapping(value = "/get/StoreOffersListRuing/{storeId}")
    public Result getStoreOffersListRuing(@PathVariable int storeId){
        return Result.successResult(storeService.getStoreOffersListRuing(storeId));
    }

    @GetMapping(value = "/update/endStoreOffers/{storeId}/{code}")
    public Result endStoreOffers(@PathVariable String code, @PathVariable int storeId){
        return Result.successResult(storeService.endStoreOffers(code,storeId));
    }


}
